const fs = require('fs');
const {
  userInfo
} = require('os');

let uid = 1;
fs.readFile('./user.json', 'utf8', function (err, data) {
  const userData = JSON.parse(data)
  userInfo = userData.filter(item.id === uid)[0];

  fs.readFile('./userCourse.json', 'utf8', function (err, data) {
    const userCourseData = JSON.parse(data),
      userId = userInfo.id,
      userCourse = userCourseData.filter(item => {
        item.uid === userId
      })[0];

    fs.readFile('./course.json', 'utf8', function (err, data) {
      const courseData = JSON.parse(data),
        userCourse = userCourse.courses;
      let _arr = []
      userCourse.map((id) => {
        courseData.map((item) => {
          if (item.id === id) {
            _arr.push(item)
          }
        })
      });
      const userCourseInfo = {
        username: userInfo.username,
        courses: _arr
      }
      fs.writeFileSync(`./${userInfo.username}.json`, JSON.stringify(userCourseInfo))
    });

  })
})